﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GamePlugin.Logics.Core;
using WindowsInput;
using WindowsInput.Native;
using GamePlugin.Logics.Data;

namespace GamePlugin.Logics.Game.Actions
{
    public class DActionRunSearchBuyAution : MachineState
    {
        public override void Initialize()
        {
            this.AddState("ClickSearch");
            this["ClickSearch"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.ClickSearch(o, e));

            this.AddState("FindItem");
            this["FindItem"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.FindItem(o, e));

            this.AddState("DirectlyBuy");
            this["DirectlyBuy"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.DirectlyBuy(o, e));

            this.AddState("TryAution");
            this["TryAution"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.TryAution(o, e));

            this.AddState("Finished");
            this["Finished"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.Finished(o, e));

            this.DefaultState = "ClickSearch";
        }

        public void ClickSearch(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            Helper.MoveCursor(ActionData.ResetPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            Helper.MoveCursor(ActionData.SearchPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            this.ChangeToState("FindItem");
        }

        public void FindItem(object o, GameTimeEventArgs e)
        {
            Helper.Sleep(1500);
            Helper.ScreenChecker.GetScreenData();

            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            Helper.MoveCursor(ActionData.ItemNotFoundTextPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            if (Helper.ScreenChecker.CheckMouseOverImage(ImageStorage.GetBitmap("ItemNotFound.bmp"), 0.98f))
            {
                this.ChangeToState("Finished");
                return;
            }

            Helper.MoveCursor(ActionData.NoneCharacterCheckPoint);
            Helper.Sleep(100);
            if (Helper.ScreenChecker.CheckMouseOverImage(ImageStorage.GetBitmap("None.bmp"), 0.98f))
            {
                this.ChangeToState("TryAution");
                return;
            }

            ChangeToState("DirectlyBuy");
        }

        public void DirectlyBuy(object o, GameTimeEventArgs e)
        {
            Helper.Log("找到並嘗試購買 ： " + Workspace.Instance.SearchItem.SearchInfoName + 
                "價格 " + Workspace.Instance.SearchItem.ItemAuctionPrice);

            // reset
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;
            Helper.MoveCursor(ActionData.FirstItemPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            // buy
            Helper.MoveCursor(ActionData.DirectlyBuyPoint);
            Helper.Sleep(300);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(300);

            if (Workspace.Instance.SearchOptionData.RunMode == (int)ERunMode.NormalMode ||
                Workspace.Instance.SearchOptionData.RunMode == (int)ERunMode.TestAllWithLowValuesMode)
            {
                Helper.MoveCursor(ActionData.DirectlyBuyPointConfirm);
                Helper.Sleep(300);
                input.Mouse.LeftButtonClick();
                Helper.Sleep(100);
            }

            // buy cancle
            Helper.MoveCursor(ActionData.DirectlyBuyPointCancel);
            Helper.Sleep(300);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(300);

            // final buy confirm
            Helper.Sleep(3000);
            Helper.MoveCursor(ActionData.FinlayBuyConfirmPoint);
            Helper.Sleep(1000);

            Helper.ScreenChecker.GetScreenData();
            if (Helper.ScreenChecker.CheckMouseOverImage(ImageStorage.GetBitmap("Confirm.bmp"), 0.95f))
            {
                Helper.Log(DateTime.Now.ToString() + " 成功購買一個：" + Workspace.Instance.SearchItem.SearchInfoName);
                input.Mouse.LeftButtonClick();
                this.ChangeToState("Finished");
                return;
            }
            Helper.Sleep(100);

            input.Mouse.LeftButtonClick();
            ChangeToState("Finished");
        }

        public void TryAution(object o, GameTimeEventArgs e)
        {
            Helper.Log("找到並嘗試競拍 ： " + Workspace.Instance.SearchItem.SearchInfoName
                + "價格 " + Workspace.Instance.SearchItem.ItemAuctionPrice);

            // reset
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;
            Helper.MoveCursor(ActionData.AutionPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            // Aution
            Helper.MoveCursor(ActionData.AutionTextPoint);
            Helper.Sleep(300);


            // Delete numbers
            for (int j = 0; j < 5; j++)
            {
                Helper.MoveCursor(ActionData.AutionTextPoint);
                input.Mouse.LeftButtonClick();
                Helper.Sleep(50);
                for (int i = 0; i < 5; i++)
                {
                    input.Keyboard.KeyDown(VirtualKeyCode.DELETE);
                    Helper.Sleep(50);
                }
            }
            Helper.MoveCursor(ActionData.AutionTextPoint);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);
            System.Windows.Forms.Clipboard.Clear();
            System.Windows.Forms.Clipboard.SetText(Workspace.Instance.SearchItem.ItemAuctionPrice.ToString());
            Helper.Sleep(1000);
            input.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V);
            Helper.Sleep(500);

            if (Workspace.Instance.SearchOptionData.RunMode == (int)ERunMode.NormalMode ||
                Workspace.Instance.SearchOptionData.RunMode == (int)ERunMode.TestAllWithLowValuesMode)
            {
                Helper.MoveCursor(ActionData.AutionConfirmPoint);
                Helper.Sleep(300);
                input.Mouse.LeftButtonClick();
                Helper.Sleep(300);
            }

            Helper.MoveCursor(ActionData.AutionCancelPoint);
            Helper.Sleep(300);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(300);

            Helper.Sleep(3000);
            Helper.MoveCursor(ActionData.FinalAutionConfirm);
            Helper.Sleep(1000);

            Helper.ScreenChecker.GetScreenData();
            if (Helper.ScreenChecker.CheckMouseOverImage(ImageStorage.GetBitmap("Confirm.bmp"), 0.95f))
            {
                Helper.Log(" 成功競拍一個：" + Workspace.Instance.SearchItem.SearchInfoName 
                    + "價格 " + Workspace.Instance.SearchItem.ItemAuctionPrice);
                input.Mouse.LeftButtonClick();
                this.ChangeToState("Finished");
                return;
            }

            input.Mouse.LeftButtonClick();
            ChangeToState("Finished");
        }

        public void Finished(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;
            Helper.MoveCursor(ActionData.ResetPoint);
            Helper.Sleep(100);
            input.Mouse.LeftButtonClick();
            Helper.Sleep(100);

            this.IsNeedReset = true;
            DActionRunItemSearchStart.FromIdleState = false;
            this.Owner.ChangeToState(typeof(DActionRunItemSearchEnd).Name);
        }
    }
}
